<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>题目管理</el-breadcrumb-item>
      <el-breadcrumb-item>题目列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 搜索和添加 -->

      <el-row :gutter="10">
        <el-col :span="7">
          <el-input
            placeholder="请输入题目内容"
            v-model="queryInfo.query"
            clearable
            @clear="getList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加题目</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="itemList" border stripe class="etable">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="题目类别" prop="cateName"></el-table-column>
        <el-table-column label="题目描述" prop="titleDes"></el-table-column>
        <el-table-column label="A" prop="answerA"></el-table-column>
        <el-table-column label="B" prop="answerB"></el-table-column>
        <el-table-column label="C" prop="answerC"></el-table-column>
        <el-table-column label="正确答案" prop="answerRight"></el-table-column>
        <el-table-column label="答案描述" prop="answerDes"></el-table-column>
        <el-table-column label="考点" prop="source"></el-table-column>
        <el-table-column
          label="难度"
          prop="level"
          width="100"
        ></el-table-column>

        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 6, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加题目对话框 -->
    <el-dialog
      title="添加题目"
      :visible.sync="addDialogVisible"
      width="35%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="ruleFormRef"
        label-width="100px"
      >
        <el-form-item label="题目类别" prop="cateName">
          <el-input v-model="addForm.cateName"></el-input>
        </el-form-item>
        <el-form-item label="题目描述" prop="titleDes">
          <el-input v-model="addForm.titleDes"></el-input>
        </el-form-item>
        <el-form-item label="A" prop="answerA">
          <el-input v-model="addForm.answerA"></el-input>
        </el-form-item>
        <el-form-item label="B" prop="answerB">
          <el-input v-model="addForm.answerB"></el-input>
        </el-form-item>
        <el-form-item label="C" prop="answerC">
          <el-input v-model="addForm.answerC"></el-input>
        </el-form-item>
        <el-form-item label="正确答案" prop="answerRight">
          <el-radio-group v-model="addForm.answerRight">
            <el-radio-button label="A"></el-radio-button>
            <el-radio-button label="B"></el-radio-button>
            <el-radio-button label="C"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="答案描述" prop="answerDes">
          <el-input v-model="addForm.answerDes"></el-input>
        </el-form-item>
        <el-form-item label="考点" prop="source">
          <el-radio-group v-model="addForm.source">
            <el-radio-button label="vue"></el-radio-button>
            <el-radio-button label="react"></el-radio-button>
            <el-radio-button label="js"></el-radio-button>
            <el-radio-button label="css"></el-radio-button>
            <el-radio-button label="html"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="难度" prop="level">
          <el-radio-group v-model="addForm.level">
            <el-radio-button label="简单"></el-radio-button>
            <el-radio-button label="中等"></el-radio-button>
            <el-radio-button label="困难"></el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addItem">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 操作按钮 -->
    <!-- 修改 -->
    <el-dialog
      title="修改题目"
      :visible.sync="editDialogVisible"
      width="30%"
      @close="editDialogVisible = false"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="题目类别" prop="cateName">
          <el-input v-model="editForm.cateName"></el-input>
        </el-form-item>
        <el-form-item label="题目描述" prop="titleDes">
          <el-input v-model="editForm.titleDes"></el-input>
        </el-form-item>
        <el-form-item label="A" prop="answerA">
          <el-input v-model="editForm.answerA"></el-input>
        </el-form-item>
        <el-form-item label="B" prop="answerB">
          <el-input v-model="editForm.answerB"></el-input>
        </el-form-item>
        <el-form-item label="C" prop="answerC">
          <el-input v-model="editForm.answerC"></el-input>
        </el-form-item>
        <el-form-item label="正确答案" prop="answerRight">
          <el-radio-group v-model="editForm.answerRight">
            <el-radio-button label="A"></el-radio-button>
            <el-radio-button label="B"></el-radio-button>
            <el-radio-button label="C"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="答案描述" prop="answerDes">
          <el-input v-model="editForm.answerDes"></el-input>
        </el-form-item>
        <el-form-item label="考点" prop="source">
          <el-input v-model="editForm.source"></el-input>
        </el-form-item>
        <el-form-item label="难度" prop="level">
          <el-radio-group v-model="editForm.level">
            <el-radio-button label="简单"></el-radio-button>
            <el-radio-button label="中等"></el-radio-button>
            <el-radio-button label="困难"></el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 <script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        //当前页数
        pagenum: 1,
        //显示多少条数据
        pagesize: 10,
      },
      itemList: [],
      total: 0,
      addDialogVisible: false,
      //添加用户
      addForm: {
        cateName: "",
        titleDes: "",
        answerA: "",
        answerB: "",
        answerC: "",
        answerRight: "A",
        answerDes: "",
        source: "css",
        level: "简单",
      },
      addFormRules: {
        cateName: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        titleDes: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerA: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerB: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerC: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerRight: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerDes: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        source: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        level: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
      },
      editDialogVisible: false,

      editForm: {},
      //修改表单验证规则
      editFormRules: {
        cateName: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        titleDes: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerA: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerB: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerC: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerRight: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        answerDes: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        source: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        level: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();

    //加点数据进去
    // this.zidong()
  },
  methods: {
    //       zidong(){
    // var num=0

    // var timer=  setInterval(()=>{
    //   num++
    //   this.addForm.titleDes='vue'+String(Math.random())
    //   this.addForm.source='vue'
    // if(num===20){
    //   clearInterval(timer)
    // }
    //   this.$http.post("/exercisesList", this.addForm)
    // },500)
    //       },
    async getList() {
      console.log(this.queryInfo.query, "query");

      const res = await this.$http.get("/exercisesList", {
        params: { query: this.queryInfo },
      });
      console.log(res);

      this.itemList = res.data.data;
      this.total = res.data.total;
    },

    handleSizeChange(newsize) {
      console.log(newsize);
      this.queryInfo.pagesize = newsize;
      this.getList();
    },
    //监听页码值变化
    handleCurrentChange(newpage) {
      console.log(newpage);
      this.queryInfo.pagenum = newpage;
      this.getList();
    },

    // //重置表单
    addDialogClosed() {
      this.$refs.ruleFormRef.resetFields();
    },

    //点击按钮，添加
    addItem() {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (!valid) return;
        const res = await this.$http.post("/exercisesList", this.addForm);
        console.log(res);
        if (res.data.state !== 200) {
          this.$message.error("添加失败！");
        }

        this.$message.success("添加成功！");
        //隐藏对话框
        this.addDialogVisible = false;
        //重新列表
        this.getList();
      });
    },

    //修改部分
    async showEditDialog(id) {
      console.log(id);
      const res = await this.$http.get("exercises/" + id);

      this.editForm = res.data.data[0];
      console.log(this.editForm);
      this.editDialogVisible = true;
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },

    //修改并且提交
    editInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        const res = await this.$http.put("exercisesList/" + this.editForm.id, {
          editForm: this.editForm,
        });

        console.log(res);
        //关闭对话框 刷新数据 提示成功
        this.editDialogVisible = false;
        this.getList();
        this.$message.success("修改成功");
      });
    },

    //删除
    async removeById(id) {
      //弹框询问是否删除
      const confirmResult = await this.$confirm(
        "此操作将永久删除该题目, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      //如果用户点击确定，返回字符串confirm
      //取消删除。返回字符串cancel
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

      const res = await this.$http.delete("/exercisesList/" + id);
      console.log(res);
      if (res.data.state !== 200) {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.getList();
    },
  },
};
</script>
    <style lang="less" scoped>
</style>